package cn.tedu.antconlonysever.contract.dao.persist.repository.impl;

import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.util.PageInfoToPageDataConverter;
import cn.tedu.antconlonysever.contract.dao.persist.mapper.OrderDetailsMapper;
import cn.tedu.antconlonysever.contract.dao.persist.repository.IOrderDetailsRepository;
import cn.tedu.antconlonysever.contract.pojo.entity.OrderDetails;
import cn.tedu.antconlonysever.contract.pojo.vo.OrderDetailsListVO;
import cn.tedu.antconlonysever.contract.pojo.vo.OrderDetailsStandardVO;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;


import java.util.Collection;
import java.util.List;

@Slf4j
@Repository
public class OrderDetailsRepositoryImpl implements IOrderDetailsRepository {
    @Autowired
    private OrderDetailsMapper orderDetailsMapper;
    @Override
    public int insert(OrderDetails orderDetails) {

        return orderDetailsMapper.insert(orderDetails);
    }

    @Override
    public int insertBatch(List<OrderDetails> orderDetailsList) {

        return orderDetailsMapper.insertBatch(orderDetailsList);
    }

    @Override
    public int deleteById(Long id) {

        return orderDetailsMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(Collection<Long> idList) {

        return orderDetailsMapper.deleteBatchIds(idList);
    }

    @Override
    public int update(OrderDetails orderDetails) {

        return orderDetailsMapper.updateById(orderDetails);
    }

    @Override
    public int count() {

        return orderDetailsMapper.selectCount(null);
    }

    @Override
    public int countOrderId(Long orderId) {
        QueryWrapper<OrderDetails> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId);
        return orderDetailsMapper.selectCount(queryWrapper);
    }

    @Override
    public int countOrderIdNotId(Long id, Long orderId) {
        QueryWrapper<OrderDetails> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_id",orderId)
                    .ne("id",id);
        return orderDetailsMapper.selectCount(queryWrapper);
    }

    @Override
    public OrderDetailsStandardVO getStandardById(Long id) {

        return orderDetailsMapper.getStandardById(id);
    }

    @Override
    public PageData<OrderDetailsListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始执行【查询订单详情列表】的数据访问，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<OrderDetailsListVO> list = orderDetailsMapper.list();
        PageInfo<OrderDetailsListVO> pageInfo = new PageInfo<>(list);
        PageData<OrderDetailsListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
